package com.chookss.home.workbench;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.Token;
import com.chookss.home.audit.clockIn.ClockInAskHolidayDetailActivity;
import com.chookss.home.audit.clockIn.ClockInReissueCardDetailActivity;
import com.chookss.home.audit.project.GroupDetailActivity;
import com.chookss.home.audit.reimbursement.ReimbursementDetailActivity;
import com.chookss.home.audit.resign.EntryDetailActivity;
import com.chookss.home.audit.resign.ResignDetailActivity;
import com.chookss.home.audit.seal.SealDetailActivity;
import com.chookss.home.audit.shop.ShopDetailActivity;
import com.chookss.home.audit.shop.ShopServiceDetailActivity;
import com.chookss.home.audit.upgrade.UpgradeDetailActivity;
import com.chookss.home.entity.WorkNewEntity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.video.VideoOnePlayActivity;
import com.chookss.video.entity.VideoEntity;
import com.chookss.view.CircleImageView;
import com.chookss.view.MyImageSpan;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.Nullable;

public class WorkAdapter extends BaseQuickAdapter<WorkNewEntity, BaseViewHolderEx> {
    private Context context;
    private String type;
    private boolean batchShow;

    public WorkAdapter(int layoutResId, Context context, @Nullable List<WorkNewEntity> data, String type, boolean batchShow) {
        super(layoutResId, data);
        this.context = context;
        this.type = type;
        this.batchShow = batchShow;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, WorkNewEntity item) {
        LinearLayout rlStatus = helper.getView(R.id.rlStatus);
        ImageView ivStatus = helper.getView(R.id.ivStatus);
        TextView tvType = helper.getView(R.id.tvType);
        TextView tvTitle = helper.getView(R.id.tvTitle);
        CircleImageView ivHead = helper.getView(R.id.ivHead);
        TextView tvName = helper.getView(R.id.tvName);
        TextView tvTime = helper.getView(R.id.tvTime);
        LinearLayout llAudit = helper.getView(R.id.llAudit);
        TextView tvAuditName = helper.getView(R.id.tvAuditName);
        TextView tvAuditTime = helper.getView(R.id.tvAuditTime);
        tvType.setText(NavigationTools.getWorkFlowName(item.getWorkFlowType()));
        GlideUtils.loadAvatar(context, item.getHeadPhotoThumbUrl(), ivHead);
        tvName.setText(item.getCreateEmployeeName());
        tvTime.setText(Utils.getAccurateDate(item.getCreateTime()));
        if ("0".equals(type)) {
            //待审核
            llAudit.setVisibility(View.GONE);
            tvTitle.setText(item.getProjectName());
            if (batchShow) {
                rlStatus.setVisibility(View.VISIBLE);
                if (item.isSelect()) {
                    ivStatus.setBackgroundResource(R.drawable.icon_fx_select);
                } else {
                    ivStatus.setBackgroundResource(R.drawable.shape_circle_dddddd_10);
                }
            } else {
                rlStatus.setVisibility(View.GONE);
            }
        } else {
            //已审核
            llAudit.setVisibility(View.VISIBLE);
            SpannableString spannableString = new SpannableString("  " + item.getProjectName());
            Drawable drawable = null;
            if ("2".equals(item.getAuditStatus())) {
                drawable = context.getResources().getDrawable(R.drawable.icon_no_pass);
            } else {
                drawable = context.getResources().getDrawable(R.drawable.icon_pass);
            }
            drawable.setBounds(
                    0,
                    0,
                    Utils.dip2px(context, 35f),
                    Utils.dip2px(context, 13f)
            );
            MyImageSpan imageSpan = new MyImageSpan(drawable);
            spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            tvTitle.setText(spannableString);
            tvAuditName.setText(item.getCreateEmployeeName() + "在" + Utils.getTimeSize(item.getAuditTime(), 0, 10) + "日审核");
            if (Utils.isNull(item.getAuditUseTime())) {
                tvAuditTime.setVisibility(View.GONE);
            } else {
                tvAuditTime.setVisibility(View.VISIBLE);
                tvAuditTime.setText("用时：" + Utils.secondChangeTime(item.getAuditUseTime()));
            }

        }

        rlStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, String> map = new HashMap<String, String>();
                if (item.isSelect()) {
                    item.setSelect(false);
                    ivStatus.setBackgroundResource(R.drawable.shape_circle_dddddd_10);
                    map.put("RefreshWork_Select", "false");
                } else {
                    item.setSelect(true);
                    ivStatus.setBackgroundResource(R.drawable.icon_fx_select);
                    map.put("RefreshWork_Select", "true");
                }
                EventBus.getDefault().post(new MyEvent(map));

            }
        });
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                // 1-新闻 2-视频 3-补卡 4-请假 5-外勤 6-图文7报销8物料9借款10补贴11用章12维修13项目交付14离职 15晋升16 回款17 修改项目金额18入职19加班
                switch (item.getWorkFlowType()) {
                    case "1":
                        toNews(item);
                        break;
                    case "2":
                        toViedeo(item.getId(), item.getProjectCode(), item.getAuditStatus());
                        break;
                    case "3":
                    case "4":
                    case "5":
                    case "14":
                    case "15":
                    case "18":
                        toClockInDetail(item);
                        break;
                    case "6":
                        toTuwen(item);
                        break;
                    case "7":
                    case "8":
                    case "9":
                    case "10":
                    case "12":
                    case "13":
                    case "16":
                    case "17":
                        toReimbursementDetail(item);
                        break;
                    case "11":
                        toSealDetail(item);
                        break;

                }
            }
        });
    }

    public void setBatchShow(boolean batchShow) {
        this.batchShow = batchShow;
    }

    //1新闻审核
    private void toNews(WorkNewEntity item) {
        Intent intent = null;
        String url = "";
        String addressType = "";
        if (Urls.baseUrl.contains("dev_kmer")) {
            addressType = "1";
        } else {
            addressType = "0";
        }
        if ("1".equals(item.getParams().getQuoteFlag())) {
            url = Urls.tuwenDetail + "mainCode=" + item.getParams().getQuoteCode() + "&newsCode=" + item.getProjectCode() + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
        } else if ("2".equals(item.getParams().getQuoteFlag())) {
            url = item.getParams().getNewsContent();
        } else {
            url = Urls.webBaseUrl + "indexNew.html#/newDetail?newsCode=" + item.getProjectCode() + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
        }
        if ("0".equals(type)) {
            intent = new Intent(context, WorkWebActivity.class);
            intent.putExtra("id", item.getId());
            intent.putExtra("projectCode", item.getProjectCode());
            intent.putExtra("type", 1);
            intent.putExtra("employeeCode", item.getAuditEmployeeCode());
        } else {
            intent = new Intent(context, WebCommonActivity.class);
        }
        intent.putExtra("url", url);
        intent.putExtra("waterMark", "13");
        context.startActivity(intent);
    }

    //2视频审核
    private void toViedeo(String id, String videoId, String auditStatus) {
        Map<String, String> map = new HashMap<>();
        map.put("videoId", videoId);
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<VideoEntity>>() {
        }.getType(), null, Urls.selectAPPVideoById, SignUtils2.makeSignStr(map), false, new ObserverCallback<VideoEntity>() {
            @Override
            public void onSuccess(VideoEntity data) {
                if (Utils.isNull(data.videoId)) {
                    MyToast.show("视频id为空");
                    return;
                }
                Intent intent = null;
                if ("0".equals(type)) {
                    intent = new Intent(context, WorkVideoActivity.class);
                    intent.putExtra("watermark", "11");
                } else {
                    if ("2".equals(auditStatus)) {
                        intent = new Intent(context, WorkVideoActivity.class);
                        intent.putExtra("auditStatus", "2");
                        intent.putExtra("watermark", "11");
                    } else {
                        intent = new Intent(context, VideoOnePlayActivity.class);
                    }

                }
                data.id = id;
                intent.putExtra("video", data);
                context.startActivity(intent);
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    //3补卡 4请假5外勤 14离职  15 晋升 18 入职
    private void toClockInDetail(WorkNewEntity item) {
        if ("4".equals(item.getWorkFlowType())) {
            Intent intent = new Intent(context, ClockInAskHolidayDetailActivity.class);
            intent.putExtra("data", item);
            context.startActivity(intent);
        } else if ("14".equals(item.getWorkFlowType())) {
            Intent intent = new Intent(context, ResignDetailActivity.class);
            intent.putExtra("data", item);
            context.startActivity(intent);
        } else if ("15".equals(item.getWorkFlowType())) {
            Intent intent = new Intent(context, UpgradeDetailActivity.class);
            intent.putExtra("data", item);
            context.startActivity(intent);
        } else if ("18".equals(item.getWorkFlowType())) {
            Intent intent = new Intent(context, EntryDetailActivity.class);
            intent.putExtra("data", item);
            context.startActivity(intent);
        } else {
            Intent intent = new Intent(context, ClockInReissueCardDetailActivity.class);
            intent.putExtra("data", item);
            intent.putExtra("type", Utils.changeNumber(item.getWorkFlowType()));
            context.startActivity(intent);
        }
    }

    //6图文审核
    private void toTuwen(WorkNewEntity item) {
        String addressType = "";
        if (Urls.baseUrl.contains("dev_kmer")) {
            addressType = "1";
        } else {
            addressType = "0";
        }
        String url = Urls.tuwenDetail + "mainCode=" + item.getProjectCode() + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
        Intent intent = null;
        if ("0".equals(type)) {
            intent = new Intent(context, WorkWebActivity.class);
            intent.putExtra("id", item.getId());
            intent.putExtra("projectCode", item.getProjectCode());
            intent.putExtra("type", 6);
            intent.putExtra("employeeCode", item.getAuditEmployeeCode());
        } else {
            intent = new Intent(context, WebCommonActivity.class);
        }
        intent.putExtra("url", url);
        intent.putExtra("waterMark", "13");
        context.startActivity(intent);
    }

    //7报销 8 物料 9 借款 10 补贴 12维修  13 项目交付 16回款  17修改项目金额
    private void toReimbursementDetail(WorkNewEntity entity) {
        Intent intent = null;
        if (7 == Utils.changeNumber(entity.getWorkFlowType()) || 9 == Utils.changeNumber(entity.getWorkFlowType()) || 10 == Utils.changeNumber(entity.getWorkFlowType())) {
            intent = new Intent(context, ReimbursementDetailActivity.class);
            intent.putExtra("data", entity);
            intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
        } else if (8 == Utils.changeNumber(entity.getWorkFlowType()) || 13 == Utils.changeNumber(entity.getWorkFlowType())) {
            intent = new Intent(context, ShopDetailActivity.class);
            intent.putExtra("goodsCode", entity.getProjectCode());
            intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
        } else if (12 == Utils.changeNumber(entity.getWorkFlowType())) {
            intent = new Intent(context, ShopServiceDetailActivity.class);
            intent.putExtra("repairCode", entity.getProjectCode());
        } else if (16 == Utils.changeNumber(entity.getWorkFlowType()) || 17 == Utils.changeNumber(entity.getWorkFlowType())) {
            intent = new Intent(context, GroupDetailActivity.class);
            intent.putExtra("repairCode", entity.getProjectCode());
            intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
        }
        if (intent != null) {
            context.startActivity(intent);
        }
    }

    //用章
    private void toSealDetail(WorkNewEntity entity) {
        Intent intent = new Intent(context, SealDetailActivity.class);
        intent.putExtra("goodsCode", entity.getProjectCode());
        intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
        intent.putExtra("data", entity);
        context.startActivity(intent);
    }

}